﻿@model PessMvc.Models.ArchivesModel

@{
    ViewBag.Title = "Paper List";
    
    bool hideArchiveDetails = false;
    hideArchiveDetails = Roles.IsUserInRole("ExpertJudge")
        && ViewBag.HideArchiveDetails != null
        && ViewBag.HideArchiveDetails == true;

    int round = -1;
    if (Request["round"] != null)
    {
        round = int.Parse(Request["round"]);
    }
}
@*@Html.Partial("_ActivityNavPartial",ViewData["ActivityID"])*@
@{
    string status="";
    if(Roles.IsUserInRole("ExpertJudge")){
         status=string.Format("（Reviewed：{0}  /  Total：{1}）", Model.PaperReviewHistory.Count, Model.PageInfo.TotalItems);
    }
}
<h2 style="width:300px;">Review Papers<span style="font-size:12px;"> <br />@status</span></h2>
<div id="operate">
    @if (round == -1)
    {
        <a href="#" class="button_current">
            All</a>
    }
    else
    {
        @Html.ActionLink("All", "PaperIndex", new { activityID = ViewData["ActivityID"], round = -1 }, null)
    }
    @if (round == 1)
    {
        <a href="#" class="button_current">Round 1</a>
    }
    else
    {
        @Html.ActionLink("Round 1", "PaperIndex", new { activityID = ViewData["ActivityID"], round = 1 }, null)
    }
    @if (round == 2)
    {
        <a href="#" class="button_current">
            Round 2
        </a>
    }
    else
    {
        @Html.ActionLink("Round 2", "PaperIndex", new { activityID = ViewData["ActivityID"], round = 2 }, null)
    }
    @if (round == 3)
    {
        <a href="#" class="button_current">
            Virtual
        </a>
    }
    else
    {
        @Html.ActionLink("Virtual", "PaperIndex", new { activityID = ViewData["ActivityID"], round = 3 }, null)
    }
</div>
<div>Total: @Model.All.Count()</div>
@*<p>
 @Html.DisplayFor(model => Model.SingleOrDefault().Subject.Activity.Name)
</p>
<div id="operate" class="formSearch" style="width:600px;">
       {/*if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") || Roles.IsUserInRole(Membership.GetUser().UserName, "CountyAdmin"))
       {
           if ((bool)ViewBag.IsArchiveModificationAllowed == true)
             {
             <div>@Html.ActionLink("New", "Create", new { activityID = ViewData["ActivityID"] }, null)</div>
             }

             if (Roles.IsUserInRole("Admin"))
             {
                 using (Html.BeginForm("ImportReviewFeedbacks", "Archive", FormMethod.Post))
                 {
                   @Html.DropDownList("SubjectID")
                   <input type="submit" value="Import Review Comments" />
                 }
             }
       }

    @if(!hideArchiveDetails)
    {
       <div>Export Proposals@Html.ActionLink("Export Proposals", "ExportArchiveList", "Archive", new {activityID=ViewData["ActivityID"]},null)</div>
   }
    @using(Html.BeginForm("Index","Archive",FormMethod.Get)){
            @Html.Hidden("ActivityID",ViewData["ActivityID"])
            @Html.TextBox("KeyWord")
            <input type="submit" value="Search" class="inputBtn" />
        }*/}
</div>*@

@using (@Html.BeginForm("StartPaperReview", "Archive", FormMethod.Post))
{    
<table id="fileList" class="oddTable">
    <thead>
    <tr>
        @if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") || Roles.IsUserInRole(Membership.GetUser().UserName, "Chair"))
        {@*<th></th>*@}
        <th class="thSN">
            ID
        </th>
        <th class="thTitle">
            Title
        </th>
        
        <th class="thSName">
            Track
        </th>
        <th class="thCName">
            Submitted Format
        </th>
<th class="thCName">
            Accepted Format
        </th>
        @if (!hideArchiveDetails) { 
        <th class="thTName">
            Authors
        </th>
        @*<th class="thLevel">
           @PessResources.Archive.GradeLevel
        </th>*@
        <th class="thSCName">
            Affiliation
        </th>
        @*<th style="width:65px">
            Reviewer Group
        </th>*@
        
        }
        @*<th style="width:65px">
            Suggestion for Paper
        </th>*@
        @if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") || Roles.IsUserInRole(Membership.GetUser().UserName, "Chair") || Roles.IsUserInRole(Membership.GetUser().UserName, "Editor"))
        {<th>
            Paper Status
         </th>
            @*<th>
            Chair's Suggestion
         </th>*@
        }
        <th class="thAction" style="width:150px"></th>
    </tr>
        </thead>
    <tbody>
@{int x = 0;}
@foreach (var item in Model.All) {
        string classname = "read";
        var pessEC = item.PaperComments.Where(ecc => ecc.ArchiveID == item.ID && ecc.ExpertSerialNumber == Membership.GetUser().UserName).FirstOrDefault();   
    
        if (User.IsInRole("ExpertJudge") )
        {
            if( pessEC == null && item.Status < 3)
            {
                classname = "unread";
            }
            if(pessEC!=null&&pessEC.PaperStatus==0)
            {
                classname += " paperreject";
            }
            else if(pessEC!=null&&pessEC.PaperStatus==1)
            {
                classname += " paperaccept";
            }
            else if (pessEC != null && pessEC.PaperStatus == 2)
            {
                classname += " paperrevision";
            }
        }
    
    if(User.IsInRole("Chair"))
    {
        if(item.PaperStatus<3)
        {
            classname = "unread";
        }
        if(item.ChairPaperStatus==0)
        {
            classname += " paperreject";
        }
        else if(item.ChairPaperStatus==1)
        {
            classname += " paperaccept";
        }
        else if(item.ChairPaperStatus==2)
        {
            classname += " paperrevision";
        }
    }                
    
    if(User.IsInRole("Editor"))
    {
        if(item.PaperStatus<5)
        {
            classname = "unread";
        }
        if (item.PaperStatus == 5)
        {
            classname += " paperreject";
        }
        else if (item.PaperStatus == 6)
        {
            classname += " paperaccept";
        }
        else if (item.PaperStatus == 7)
        {
            classname += " paperrevision";
        }
    }
        <tr class="@classname">
    

        @if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") || Roles.IsUserInRole(Membership.GetUser().UserName, "Chair"))
        { @*<td>
            
            @Html.CheckBox("Archives["+x+"].IsSelected")
            @Html.Hidden("Archives["+x+"].ArchiveID",item.ID)
           
        </td>*@ }
        <td class="thSN">
            @Html.DisplayFor(modelItem => item.CalculatedSerialNumber)
        </td>
        <td class="thTitle">
            @Html.DisplayFor(modelItem=>item.ProjectTitle)
        </td>
        <td class="thSName" title="@(Html.DisplayFor(modelItem=>item.Subject.Track.Title))">
            @Html.DisplayFor(modelItem => item.Subject.Track.ShortTitle)
        </td>
        <td class="thCName">
                @Html.DisplayFor(modelItem => item.County.Name)
        </td>
<td class="thCName">
                @Html.DisplayFor(modelItem => item.County1.Name)
        </td>
        @if (!hideArchiveDetails)
        {
        <td class="thTName" style="white-space: nowrap;">
            
                @foreach (var pa in item.PAuthors)
                {
                    @Html.DisplayFor(modelItem => pa.Name) <br />
                }
            
        </td>

        @*<td class="thLevel">
            @Html.DisplayFor(modelItem => item.GradeLevel)
        </td>*@
        <td class="thSCName">
            @if(!hideArchiveDetails)
            {
                @Html.DisplayFor(modelItem =>item.SchoolName)
            }
        </td>    
        @*<td>
            @if(!hideArchiveDetails)
            {
                if(item.ExpertGroup!=null){@item.ExpertGroup.Callsign}
            }
        </td>*@   
        }
        
        @*<td style="text-align:center;text-wrap:none;width:70px;">
            @if (Roles.IsUserInRole(Membership.GetUser().UserName, "ExpertJudge"))
            {
                //var pessEC = item.PaperComments.Where(ecc => ecc.ArchiveID == item.ID && ecc.ExpertSerialNumber == Membership.GetUser().UserName).FirstOrDefault();
                if (pessEC != null)
                {
                    if (pessEC.PaperStatus == 1)
                    {
                        <img src="@Url.Content("~/images/green20.png")" alt="Accepted" title="Accepted" />;
                    }
                    else if(pessEC.PaperStatus==2)
                    {
                        <img src="@Url.Content("~/images/yellow20.png")" alt="Rejected" title="Rejected" />;
                    }
                    else
                    {
                        <img src="@Url.Content("~/images/red20.png")" alt="Rejected" title="Rejected" />;
                    }
                }
            }
            else if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") || Roles.IsUserInRole(Membership.GetUser().UserName, "Chair")|| Roles.IsUserInRole(Membership.GetUser().UserName, "Editor"))
            {
                var pessecs = item.PaperComments;
                foreach (var pessec in pessecs)
                {
                    if (pessec.PaperStatus == 1)
                    {
                        <img src="@Url.Content("~/images/green15.png")" alt="Accepted" title="Accepted" />;
                    }
                    else if(pessec.PaperStatus==2)
                    {
                        <img src="@Url.Content("~/images/yellow15.png")" alt="Rejected" title="Rejected" />;
                    }
                    else
                    {
                        <img src="@Url.Content("~/images/red15.png")" alt="Rejected" title="Rejected" />;
                    } 
                }
            }
        </td>*@

        @if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") || Roles.IsUserInRole(Membership.GetUser().UserName, "Chair") || Roles.IsUserInRole(Membership.GetUser().UserName, "Editor"))
        {<td>
            @item.PaperStatusName
         </td>
            @*<td>
            @item.ChairPaperStatusName
         </td>*@
        }
        <td class="thAction" style="text-align:left">
        @if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") )
        {
            @Html.ActionLink("Details", "DetailsPaper", new { id = item.ID, round = round })
        }
        @if (Roles.IsUserInRole(Membership.GetUser().UserName, "Chair"))
        {
            @Html.ActionLink("Review", "DetailsPaper", new { id = item.ID, round = round })
            if (item.PaperStatus == 2 && item.ChairPaperStatus < 3)
            {
                <span style="color:red;font-weight:bolder" title="You need to submit it!">!</span>
            }
            /*if(item.PaperStatus<3)
            {
                @Html.ActionLink("Accept", "AcceptPaper", new { id = item.ID, accept=3 })
                @Html.ActionLink("Revise", "AcceptPaper", new { id = item.ID, accept=4 })
                @Html.ActionLink("Reject", "AcceptPaper", new { id = item.ID, accept=5 })
            }*/
        }
        @if (Roles.IsUserInRole(Membership.GetUser().UserName, "Editor"))
        {
            @Html.ActionLink("Review", "DetailsPaper", new { id = item.ID })

            if (item.PaperStatus == 5)
            {
                @Html.ActionLink("Email", "SendPaperStatusNotificationEmail", new { id = item.ID })
            }

            /*if(item.PaperStatus<6)
            {
                @Html.ActionLink("Accept", "AcceptPaperFinal", new { id = item.ID, accept=6 })
                @Html.ActionLink("Revise", "AcceptPaperFinal", new { id = item.ID, accept=7 })
            }*/
        }
            
        @if(Roles.IsUserInRole(Membership.GetUser().UserName,"ExpertJudge"))
        {

            if (ViewBag.IsPaperReviewAllowed)
            {
                //@Html.ActionLink("评分", "Grade", new { archiveID=item.ID })
                if(item.PaperStatus>=2)
                {
                    @Html.ActionLink("Review", "DetailsPaper", new { id = item.ID, round = round });
                    if (Model.PaperReviewHistory.Select(r => r.ArchiveID).Contains(item.ID))
                    {
                        //<img src="@Url.Content("~/images/check20.png")" alt="Reviewed" title="Reviewed" />;
                    }
                    else
                    {
                        <img src="@Url.Content("~/images/new40.png")" alt="Unreviewed" title="Unreviewed" />;
                    }
                }

                
            }
        }
        </td>
    </tr>
    {x = x + 1;}
}
</tbody>
</table>



<div id="pageNum">@Html.PageLinks(Model.PageInfo,u=>Url.Action("Index","Archive",new {ActivityID=Model.ActivityID,SubjectID=Model.SubjectID,Keyword=Model.Keyword,Page=u}))</div>

    if (Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") || Roles.IsUserInRole(Membership.GetUser().UserName, "Chair"))
    {
<div style="padding-top: 20px; clear: both">
    @* <span>Set paper status to: </span>

        @Html.DropDownList("ExpertGroupID",new SelectList(Model.ExpertGroups,"ID","Callsign"))*@

    @Html.Hidden("Page", Model.PageInfo.CurrentPage)
    @Html.Hidden("activityid", ViewData["ActivityID"])
    @Html.Hidden("round",round)
    @if (User.IsInRole("Chair"))
    {
        <input type="submit" value="Start Paper Review" class="asBtn" />}
</div>
}

}

<div id="dialog-message" title="Message">
    <p>
        <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;">Message</span>
        <span id="msg_in_dialog"></span>
    </p>
</div>

<script type="text/javascript">
          $(document).ready(function () {

         $("#dialog-message").hide();

          $("form").submit(function (event) {
              event.preventDefault();
              //alert("test");
              $(this).validate().defaultShowErrors();
              if ($(this).valid()) {
                  // Show the "busy" Gif:
                  $('#msg_in_dialog').html("Processing...<br/><br/><img src='@Url.Content("~/images/ajax-loader.gif")'>");
                  $("#dialog-message").dialog({
                      modal: true
                  });

                  var url = $(this).attr("action");
                  var formData = $(this).serialize();
                  $.ajax({
                      url: url,
                      type: "POST",
                      data: formData,
                      dataType: "json",
                      success: function (url) {
                          // Hide the "busy" gif:
                          $('#msg_in_dialog').html("Changes saved. <br/>Start paper review reminder emails sent.");
                          $("#dialog-message").dialog({
                              modal: true,
                              buttons: {
                                  "Close": function () {
                                      window.location.href = url;
                                      $(this).dialog("close");

                                  }
                              }
                          });
                          // Do something useful with the data:
                      }
                  })
              }
              else
              {
                  //$(".error").show();
                  $('#msg_in_dialog').html("Please complete all the fields.");
                  $("#dialog-message").dialog({
                      modal: true,
                      buttons: {
                          "Close": function () {
                              $(this).dialog("close");
                          }
                      }
                  });
              }
          });

  });

</script>
